从零开始接入阿里云IP地理位置库 您所在的位置:网站首页 ip 地理位置 从零开始接入阿里云IP地理位置库

从零开始接入阿里云IP地理位置库

2023-03-21 05:52| 来源: 网络整理| 查看: 265

产品对接指南

1、API对接常用参数(后文产品对接中会用到)

获取AccessKeyID、Secret

获取实例ID(即InstanceId)

regionId目前仅支持cn-hangzhou节点,国外用户强烈不推荐使用在线版

2、API快速入门

3、API在线调试

产品使用指南注意

本部分主要介绍代码如何使用及演示,如果已经看过产品文档,已理解如何使用,可忽略以下文档,或直接查看离线库升级文档。

1、IP地理位置库(在线版)对接指南

在线版服务支持用户调用阿里云API获取IP地理定位能力,在具体对接时,我们建议您通过集成阿里云开放平台SDK来调用服务(前往下载SDK)。

下文中以集成java版本阿里云SDK为例,演示如何对接在线版IP地理位置库服务:

(1)、使用idea开发工具创建Maven全新项目

(2)、添加jar包依赖,下载jar文件坐标

(3)、创建com.example包下的DescribeIpv4Online.java文件,填写AK信息(即前文提到的AccessKeyID、Secret)

package com.example; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.profile.DefaultProfile; import com.google.gson.Gson; import java.util.*; import com.aliyuncs.geoip.model.v20200101.*; public class DescribeIpv4Online { public static void main(String[] args) { DefaultProfile profile = DefaultProfile.getProfile( "cn-hangzhou", //此处配置regionId(固定是cn-hangzhou) "AccessKeyID", "Secret" ); IAcsClient client = new DefaultAcsClient(profile); DescribeIpv4LocationRequest request = new DescribeIpv4LocationRequest(); request.setIp("47.100.XX.XX"); try { DescribeIpv4LocationResponse response = client.getAcsResponse(request); System.out.println(new Gson().toJson(response)); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { System.out.println("ErrCode:" + e.getErrCode()); System.out.println("ErrMsg:" + e.getErrMsg()); System.out.println("RequestId:" + e.getRequestId()); } } }

(4)、运行DescribeIpv4Online.java示例,在控制台得到以下结果

{ "requestId": "8BEE7785-0755-420A-9EF7-1F3BCE5F53F6", "ip": "47.100.XX.XX", "country": "中国", "province": "黑龙江省", "city": "牡丹江市", "county": "阳明区", "isp": "中国联通", "countryCode": "CN", "countryEn": "China", "provinceEn": "Heilongjiang", "cityEn": "Mudanjiang", "longitude": "129.634645", "latitude": "44.596328" }

此时返回值是小驼峰格式,如果想返回标准API大驼峰格式可以使用以下代码代替,或者直接使用response.getCity()等方法返回具体值,不用考虑字段大小写。

HttpResponse httpResponse = client.doAction(request); System.out.println("responseURL:\n" + new String(httpResponse.getHttpContent()));

2、IP地理位置库(离线版)对接指南

注意

为了不发生混乱我们重新创建个Maven项目用于离线版的演示

离线版服务支持用户将加密IP库离线下载到本地,在本地搭建服务使用(前往下载离线SDK)。

购买开通服务后可以在控制台下载文件/API调用(详情参考离线升级文档)得到以下三个文件

.lic为授权文件

.dex为离线库文件

离线SDK文件(选择需要的开发语言版本即可,下文以java版本为例演示对接)

例如:离线

下文基于以上3个文件(SDK以java版本做演示),开始演示如何使用。

集成sdk方式:

(1)、复制jar文件到项目中,文件位置可自行设置,并添加引用

File > Project Structure > Modules > Dependencies > + > JARs or directories

添加commons-lang3、guava、commons-codec、fastjson、slf4j-api的Maven坐标并下载

4.0.0 com.example untitled1 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.1.5.RELEASE 1.8 sonatype-nexus-staging Sonatype Nexus Staging https://oss.sonatype.org/service/local/staging/deploy/maven2/ true true org.springframework.boot spring-boot-starter-web org.apache.commons commons-lang3 3.10 com.google.guava guava 28.2-jre commons-codec commons-codec 1.11 com.alibaba fastjson 1.2.83 org.slf4j slf4j-api 1.7.26

(2)、创建com.example.DescribeIpv4Offline.java,配置授权文件和离线库文件访问路径

package com.example; import com.alibaba.sec.client.FastIPGeoClient; import com.alibaba.sec.domain.FastGeoConf; import com.alibaba.sec.exception.FastIPGeoException; import com.alibaba.sec.license.exception.LicenseException; import org.springframework.stereotype.Service; @Service public class DescribeIpv4Offline { private static final String DATA_FILE_PATH = "v1.20201013183640.dex"; private static final String LICENSE_FILE_PATH = "v1.20201013183640.lic"; public static void main(String[] args) throws Exception { FastGeoConf geoConf = new FastGeoConf(); geoConf.setDataFilePath(DATA_FILE_PATH); geoConf.setLicenseFilePath(LICENSE_FILE_PATH); //普通对象方式 //FastIPGeoClient fastIpGeoClient = new FastIPGeoClient(geoConf); //单例模式 FastIPGeoClient fastIpGeoClient = FastIPGeoClient.getSingleton(geoConf); String result = null; try { result = fastIpGeoClient.search("47.100.XX.XX"); System.out.println(result); } catch (LicenseException | FastIPGeoException e) { e.printStackTrace(); } } }

运行DescribeIpv4Offline.java示例,在控制台得到以下结果

{ "country": "中国", "city": "牡丹江市", "county_code": "231003", "isp": "中国联通", "province_en": "Heilongjiang", "latitude": "44.596328", "county": "阳明区", "country_en": "China", "city_code": "231000", "province_code": "230000", "city_en": "Mudanjiang", "country_code": "CN", "routes": "中国联通", "province": "黑龙江省", "isp_code": "100026", "longitude": "129.634645" }

SpringBoot方式:

注意

SpringBoot方式仅仅是为了演示方便使用,具体业务要根据情况而定,http/rpc方式对性能以及qps有很大损失

使用idea的JBLSpringBootAppGen插件创建启动引导类Application

(1)、创建com.example.service.GeoipService,配置授权文件和离线库文件访问路径

package com.example.service; import com.alibaba.sec.client.FastIPGeoClient; import com.alibaba.sec.domain.FastGeoConf; import com.alibaba.sec.exception.FastIPGeoException; import com.alibaba.sec.license.exception.LicenseException; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; @Service public class GeoipService { private static final String DATA_FILE_PATH = "v1.20201013183640.dex"; private static final String LICENSE_FILE_PATH = "v1.20201013183640.lic"; private FastIPGeoClient fastIpGeoClient ; @PostConstruct public void init() throws Exception { FastGeoConf geoConf = new FastGeoConf(); geoConf.setDataFilePath(DATA_FILE_PATH); geoConf.setLicenseFilePath(LICENSE_FILE_PATH); //普通对象 //fastIpGeoClient = new FastIPGeoClient(geoConf); //单例模式 fastIpGeoClient = FastIPGeoClient.getSingleton(geoConf); } public String searchIpv4(String ip) throws Exception { String result = null; try { result = fastIpGeoClient .search(ip); } catch (LicenseException | FastIPGeoException e) { e.printStackTrace(); } return result; } }

(2)、创建com.example.controller.GeoipController.java

package com.example.controller; import com.example.service.GeoipService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/http") public class GeoipController { @Autowired private GeoipService geoipService; @GetMapping("/offline/ipv4/{ip}") public String offline(@PathVariable String ip) throws Exception { return geoipService.searchIpv4(ip); } }

离线版项目结构参考离线目录结构

(3)、启动SpringBoot项目,在浏览器访问

http://127.0.0.1:8080/http/offline/ipv4/47.100.XX.XX

离线结果


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

      专题文章
        CopyRight 2018-2019 实验室设备网 版权所有